#include<cstdio>
const int MAXN=1e5+10;
struct Splay{
    int l,r;
    int val;
    int size;
    int cnt;
};
Splay tree[MAXN];
int cnt,root;
inline void newnode(int &nw,int val)
{
    tree[nw=++cnt].val=val;
    tree[nw].size++;tree[nw].cnt++;
    return;
}
inline void update(int nw)
{
    tree[nw].size=tree[tree[nw].l].size+tree[tree[nw].r].size+tree[nw].cnt;
    return;
}
inline void zig(int &nw)
{
    int l=tree[nw].l;
    tree[nw].l=tree[l].r;
    tree[l].r=nw;
    nw=l;
    update(tree[nw].r),update(nw);
    return;
}
inline void zag(int &nw)
{
    int r=tree[nw].r;
    tree[nw].r=tree[r].l;
    tree[r].l=nw;
    nw=r;
    update(tree[nw].l);update(nw);
    return;
}
void splaying(int x,int &y)
{
    if(x==y)return;
    int &l=tree[y].l,&r=tree[y].r;
    if(x==l)zig(y);
    else if(x==r)zag(y);
    else
    {
        if(tree[x].val<tree[y].val)
        {
            if(tree[x].val<tree[l].val)
                splaying(x,tree[l].l),zig(y),zig(y);
            else splaying(x,tree[l].r),zag(l),zig(y);
        }
        else
        {
            if(tree[x].val>tree[r].val)
                splaying(x,tree[r].r),zag(y),zag(y);
            else splaying(x,tree[r].l),zig(r),zag(y);
        }
    }
}
inline void delnode(int nw)
{
    splaying(nw,root);
    if(tree[nw].cnt>1)tree[nw].size--,tree[nw].cnt--;
    else if(tree[root].r)
    {
        int p=tree[root].r;
        while(tree[p].l)p=tree[p].l;
        splaying(p,tree[root].r);
        tree[tree[root].r].l=tree[root].l;
        root=tree[root].r;
        update(root);
    }
    else root=tree[root].l;
}
void ins(int &nw,int val)
{
    if(!nw)newnode(nw,val),splaying(nw,root);
    else if(val<tree[nw].val)ins(tree[nw].l,val);
    else if(val>tree[nw].val)ins(tree[nw].r,val);
    else tree[nw].size++,tree[nw].cnt++,splaying(nw,root);
}
void del(int nw,int val)
{
    if(tree[nw].val==val)delnode(nw);
    else if(val<tree[nw].val)del(tree[nw].l,val);
    else del(tree[nw].r,val);
}
int getrank(int val)
{
    int nw=root,rank=1;
    while(nw)
    {
        if(tree[nw].val==val)
        {
            rank+=tree[tree[nw].l].size;
            splaying(nw,root);
            break;
        }
        if(val<=tree[nw].val)
            nw=tree[nw].l;
        else
        {
            rank+=tree[tree[nw].l].size+tree[nw].cnt;
            nw=tree[nw].r;
        }
    }
    return rank;
}
int getnum(int rank)
{
    int nw=root;
    while(nw)
    {
        int lsize=tree[tree[nw].l].size;
        if(lsize+1<=rank&&rank<=lsize+tree[nw].cnt)
        {
            splaying(nw,root);
            break;
        }
        else if(lsize>=rank)
            nw=tree[nw].l;
        else
        {
            rank-=lsize+tree[nw].cnt;
            nw=tree[nw].r;
        }
    }
    return tree[nw].val;
}
int n;
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        switch(op)
        {
            case 1:ins(root,x);break;
            case 2:del(root,x);break;
            case 3:printf("%d\n",getrank(x));break;
            case 4:printf("%d\n",getnum(x));break;
            case 5:printf("%d\n",getnum(getrank(x)-1));break;
            default:printf("%d\n",getnum(getrank(x+1)));break;
        }
    }
    return 0;
}
//luogu 普通平衡树